#include "app/app_main.h"
#include "debug.h"

#define DEMO_STACK_SIZE				512
#define DEMO_BYTE_POOL_SIZE 	9120

TX_THREAD thread_0;
TX_THREAD thread_1;

static void thread_0_entry(ULONG thread_input);
static void thread_1_entry(ULONG thread_input);

VOID tx_application_define(VOID *first_unused_memory)
{
	CHAR *pointer = TX_NULL;
	TX_BYTE_POOL byte_pool_0;

	/* Create a byte memory pool from which to allocate the thread stacks.  */
	tx_byte_pool_create(&byte_pool_0, "byte pool 0", first_unused_memory, DEMO_BYTE_POOL_SIZE);

	/* Put system definition stuff in here, e.g. thread creates and other assorted
		 create information.  */

	/* Allocate the stack for thread 0.  */
	tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);

	/* Create the main thread.  */
	tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,  
					pointer, DEMO_STACK_SIZE, 
					1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);


	/* Allocate the stack for thread 1.  */
	tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);

	/* Create threads 1 and 2. These threads pass information through a ThreadX 
		 message queue.  It is also interesting to note that these threads have a time
		 slice.  */
	tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,  
					pointer, DEMO_STACK_SIZE, 
					16, 16, 4, TX_AUTO_START);
}

void thread_0_entry(ULONG thread_input)
{
	/* This thread simply sits in while-forever-sleep loop.  */
	while(1)
	{
		PRINTF("thread 0 is running..\r\n");
		PRINTF("tick:%d\r\n",HAL_GetTick());
		
		tx_thread_sleep(1000);
	}
}

void thread_1_entry(ULONG thread_input)
{
	/* This thread simply sends messages to a queue shared by thread 2.  */
	while(1)
	{
		PRINTF("thread 1 is running..\r\n");
		
		tx_thread_sleep(500);
	}
}
